package com.nbx.app.zeuscmdport;


import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.opengl.GLU;
import android.view.MotionEvent;

/**
 * http://www.zeuscmd.com/tutorials/opengles/15-Lighting.php
 * 
 *
 */
public class Render15 extends RenderBase {

	private float xrot = 0.0f;
	private float yrot = 0.0f;
	
	private boolean bMatEnable = false;

	private float box[] = {
		// FRONT
		-0.5f, -0.5f,  0.5f,
		 0.5f, -0.5f,  0.5f,
		-0.5f,  0.5f,  0.5f,
		 0.5f,  0.5f,  0.5f,
		// BACK
		-0.5f, -0.5f, -0.5f,
		-0.5f,  0.5f, -0.5f,
		 0.5f, -0.5f, -0.5f,
		 0.5f,  0.5f, -0.5f,
		// LEFT
		-0.5f, -0.5f,  0.5f,
		-0.5f,  0.5f,  0.5f,
		-0.5f, -0.5f, -0.5f,
		-0.5f,  0.5f, -0.5f,
		// RIGHT
		 0.5f, -0.5f, -0.5f,
		 0.5f,  0.5f, -0.5f,
		 0.5f, -0.5f,  0.5f,
		 0.5f,  0.5f,  0.5f,
		// TOP
		-0.5f,  0.5f,  0.5f,
		 0.5f,  0.5f,  0.5f,
		 -0.5f,  0.5f, -0.5f,
		 0.5f,  0.5f, -0.5f,
		// BOTTOM
		-0.5f, -0.5f,  0.5f,
		-0.5f, -0.5f, -0.5f,
		 0.5f, -0.5f,  0.5f,
		 0.5f, -0.5f, -0.5f,
	};

	private float lightAmbient[] = { 0.2f, 0.3f, 0.6f, 1.0f };
	private float lightDiffuse[] = { 0.2f, 0.3f, 0.6f, 1.0f };

	private float matAmbient[] = { 0.6f, 0.6f, 0.6f, 1.0f };
	private float matDiffuse[] = { 0.6f, 0.6f, 0.6f, 1.0f };

	private FloatBuffer mBoxBuffer;
	
	private FloatBuffer mLightAmbientBuffer;
	private FloatBuffer mLightDiffuseBuffer;
	
	private FloatBuffer mMatAmbientBuffer;
	private FloatBuffer mMatDiffuseBuffer;
	
	public Render15(Context context) {
		super(context);
	}

	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		mBoxBuffer = createFloatBuffer(box);
		
		mLightAmbientBuffer = createFloatBuffer(lightAmbient);
		mLightDiffuseBuffer = createFloatBuffer(lightDiffuse);
		mMatAmbientBuffer = createFloatBuffer(matAmbient);
		mMatDiffuseBuffer = createFloatBuffer(matDiffuse);
		
		gl.glEnable(GL10.GL_LIGHTING);
		gl.glEnable(GL10.GL_LIGHT0);
		gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_AMBIENT, mMatAmbientBuffer);
		gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_DIFFUSE, mMatDiffuseBuffer);

		gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_AMBIENT, mLightAmbientBuffer);
		gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_DIFFUSE, mLightDiffuseBuffer);

		gl.glEnable(GL10.GL_DEPTH_TEST);
		gl.glDepthFunc(GL10.GL_LEQUAL);

		gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
		gl.glClearDepthf(1.0f);

		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mBoxBuffer);
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

		gl.glEnable(GL10.GL_CULL_FACE);
		gl.glShadeModel(GL10.GL_SMOOTH);
	}

	public void onSurfaceChanged(GL10 gl, int width, int height) {
		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glLoadIdentity();

		gl.glViewport(0, 0, width, height);
		GLU.gluPerspective(gl, 45.0f, 1.0f * width / height, 1.0f, 100.0f);

		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glLoadIdentity();
	}

	public void onDrawFrame(GL10 gl) {
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		gl.glLoadIdentity();
		
		if (bMatEnable) {
			
			gl.glEnable(GL10.GL_COLOR_MATERIAL);
		} else {
			gl.glDisable(GL10.GL_COLOR_MATERIAL);
		}		

		GLU.gluLookAt(gl, 0.0f, 0.0f, 3.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);

		gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f);
		gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f);
		
		// FRONT AND BACK
		gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
		gl.glNormal3f(0.0f, 0.0f, 1.0f);
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
		gl.glNormal3f(0.0f, 0.0f, -1.0f);
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 4, 4);

		// LEFT AND RIGHT
		gl.glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
		gl.glNormal3f(-1.0f, 0.0f, 0.0f);
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 8, 4);
		gl.glNormal3f(1.0f, 0.0f, 0.0f);
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 12, 4);

		// TOP AND BOTTOM
		gl.glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
		gl.glNormal3f(0.0f, 1.0f, 0.0f);
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 16, 4);
		gl.glNormal3f(0.0f, -1.0f, 0.0f);
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 20, 4);

		gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
		
		xrot += 2.0f;
		yrot += 3.0f;
	}
	
	@Override
	public boolean handleTouchEvent(MotionEvent event) {
		if(event.getAction() == MotionEvent.ACTION_UP){
			bMatEnable = !bMatEnable;
		}
		return true;
	}
}